package com.itsci.assignment1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditPersonHistory extends Activity {
	private EditText name;
	private EditText address;
	private EditText phone;
	private EditText birthday;
	private EditText height;
	private EditText age;
	private EditText weight;
	private EditText appear;
	private EditText be;
	private Button btn;
	private Button clear;
	private String intent_name;
	private final String path = "/mnt/sdcard/TextFile/Patient.txt";
	private String text[];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_patient);
		
		name = (EditText)findViewById(R.id.editText_Edit_Name_Patient);
		address =(EditText)findViewById(R.id.editText_Edit_Patient_Address);
		phone =(EditText)findViewById(R.id.editText_Edit_Patient_Phone);
		birthday = (EditText)findViewById(R.id.editText_Edit_Patient_Birthday);
		height = (EditText)findViewById(R.id.editText_Edit_Patient_Height);
		age = (EditText)findViewById(R.id.editText_Edit_Patient_Age);
		weight = (EditText)findViewById(R.id.editText_Edit_Patient_Weight);
		appear = (EditText)findViewById(R.id.editText_Edit_Patient_Appearance);
		be = (EditText)findViewById(R.id.editText_Edit_Patient_Be);
		
		Intent intent = getIntent();
		intent_name = intent.getStringExtra("name");
		name.setText(intent.getStringExtra("name"));
		address.setText(intent.getStringExtra("address"));
		phone.setText(intent.getStringExtra("phone"));
		birthday.setText(intent.getStringExtra("birthday"));
		height.setText(intent.getStringExtra("height"));
		age.setText(intent.getStringExtra("age"));
		weight.setText(intent.getStringExtra("weight"));
		appear.setText(intent.getStringExtra("appear"));
		be.setText(intent.getStringExtra("be"));
		btn = (Button)findViewById(R.id.button_SavePatient);
		clear =(Button)findViewById(R.id.button_ClearEditPatient);
		btn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				String line="";
				text = ReadFileForSave();
				
				for(int i=0;i<text.length;i++){
					if(text[i].split(",")[0].equalsIgnoreCase(intent_name)){
						String temp[] = text[i].split(",");
						temp[0] = name.getText().toString();						
						temp[1] = birthday.getText().toString();
						temp[2] = age.getText().toString();
						temp[3] = height.getText().toString();
						temp[4] = weight.getText().toString();
						temp[5] = address.getText().toString();
						temp[6] = phone.getText().toString();				
						temp[7] = appear.getText().toString();
						temp[8] = be.getText().toString();
						line += temp[0]+","+temp[1]+","+temp[2]+","+temp[3]+","+temp[4]+","+temp[5]+","+temp[6]+","+temp[7]+","+temp[8]+"\n";
					}else{
						line += text[i]+"\n";
					}
				}				
				try {

					File file = new File(path);
					if (!file.exists()) {
						try {
							file.createNewFile();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					FileWriter writer = new FileWriter(file);
					writer.write(line);
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(EditPersonHistory.this, "Failed!" + e.getMessage(),
							Toast.LENGTH_LONG).show();

				}
				Intent gotoEdit= new Intent(EditPersonHistory.this,ShowPersonalHistory.class);
				gotoEdit.putExtra("name",name.getText().toString());				
				gotoEdit.putExtra("birthday",birthday.getText().toString());
				gotoEdit.putExtra("age",age.getText().toString());
				gotoEdit.putExtra("height",height.getText().toString());
				gotoEdit.putExtra("weight",weight.getText().toString());
				gotoEdit.putExtra("address",address.getText().toString());
				gotoEdit.putExtra("phone",phone.getText().toString());
				gotoEdit.putExtra("appear",appear.getText().toString());
				gotoEdit.putExtra("be",be.getText().toString());
				setResult(111,gotoEdit);
				finish();
//				startActivity(gotoEdit);
			
			}
		});
		clear.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				name.setText("");
				address.setText("");
				phone.setText("");
				birthday.setText("");
				height.setText("");
				age.setText("");
				weight.setText("");
				appear.setText("");
				be.setText("");
			}
		});

	}
	public String[] ReadFileForSave() {
		ArrayList<String> temp = new ArrayList<String>();
		try {
			File myFile = new File(path);
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(
					fIn));
			String aDataRow = "";
			@SuppressWarnings("unused")
			int i=0;
			while ((aDataRow = myReader.readLine()) != null) {
				temp.add(aDataRow);
				i++;
			}
			myReader.close();
		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}
		return (String[]) temp.toArray(new String[temp.size()]);
	}

}
